package com.lingnan.controller;

import com.lingnan.entity.Clazz;
import com.lingnan.service.ClazzFeignService;
import com.lingnan.service.ClazzService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @PostMapping("insert")
    public Object insert(Clazz bean) {
        return clazzService.insert(bean);
    }

    @GetMapping("queryAll")
    public Object queryAll() {
        return clazzService.queryAll();
    }

    @GetMapping("queryById/{id}")
    @HystrixCommand(fallbackMethod="getDefaultClazz")
    public Object queryById(@PathVariable  Integer id) {
        Clazz clazz = clazzService.queryById(id);
        if(clazz == null){
            throw new RuntimeException("出错了，没有该id！");
        }
        return clazz;
    }

    private Object getDefaultClazz(@PathVariable Integer id){
        return new Clazz().setId(id).setName("班级不存在").setDb("no db");
    }

}
